package com.mdhw.guanlin.service.impl;

import com.mdhw.common.base.MiliPageData;
import com.mdhw.common.contants.CacheKeyContants;
import com.mdhw.config.R;
import com.mdhw.guanlin.service.GuanUserService;
import com.mdhw.guanlin.service.GuanlinService;
import com.mdhw.guanlin.vo.GuanUserVo;
import com.mdhw.mili.service.MiliService;
import com.mdhw.mili.vo.area.CityVo;
import com.mdhw.mili.vo.area.ParksVo;
import com.mdhw.mili.vo.area.VisitorVo;
import com.mdhw.mili.vo.system.DictDataVo;
import com.mdhw.mili.vo.system.FaceCheckVo;
import com.mdhw.mili.vo.user.*;
import com.mdhw.utils.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class GuanlinServiceImpl implements GuanlinService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MiliService miliService;
    @Autowired
    private GuanUserService guanUserService;

    /**
     * 查询小区
     * @param current
     * @param size
     * @param name
     * @param provinceCode
     * @param cityCode
     * @param countyCode
     * @param streetCode
     * @param userId
     * @param type
     * @param cityName
     * @return
     */
    @Override
    public R<MiliPageData<VisitorVo>> selectAllVisitorPage(Integer current, Integer size, String name, String provinceCode, String cityCode, String countyCode, String streetCode, Long userId, String type, String cityName) {
        String miliToken = this.selectMiliToken();

        return this.miliService.selectAllVisitorPage(current,size,name,provinceCode,cityCode,countyCode,streetCode,userId,type,cityName,miliToken);

    }

    @Override
    public R<FangkeVo> selectFangkeVoByMobile(String fangkeMobile,String projectId) {
        String miliToken = this.selectMiliToken();

        R<FangkeVo> returnVo = this.miliService.selectFangkeByMobile(fangkeMobile, miliToken, projectId);
        return returnVo;
    }


    @Override
    public List<CityVo> selectCityList(String name, String provinceCode, String cityCode, String streetCode, String cityName, String projectId) {
        String miliToken = this.selectMiliToken();
        List<CityVo> cityVoList = this.miliService.selectCityList(name,provinceCode,cityCode,streetCode,cityName, miliToken, projectId);
        return cityVoList;
    }


    @Override
    public R<List<YezhuVo>> selectYezhuVo(String name, String projectId) {
        String miliToken = this.selectMiliToken();
        return this.miliService.selectYezhuVo(name, miliToken, projectId);
    }

    @Override
    public List<ParksVo> selectParksList(String projectId) {
        String miliToken = this.selectMiliToken();
        List<ParksVo> parksVoList = this.miliService.selectParksList( miliToken, projectId);
        return parksVoList;
    }

    @Override
    public R<List<DictDataVo>> selectMiliDictDataList(String dictType) {
        String miliToken = this.selectMiliToken();
        R<List<DictDataVo>> returnVo = this.miliService.selectDictDataList(dictType ,miliToken);
        return returnVo;
    }

    @Override
    public Boolean visitoraddVisitorByPhone(String phone ,String projectId) {
        String miliToken = this.selectMiliToken();
        if(StringUtils.isBlank(phone)){
            GuanUserVo guanUserVo = this.guanUserService.selectUserByToken();
            phone = guanUserVo.getPhone();
        }
        //Boolean addSuccess = this.miliService.visitoraddVisitorByPhone(phone, miliToken, projectId);
        return Boolean.FALSE;
    }

    @Override
    public R registerByVisitor(FangkeRegisterVo fangkeRegisterVo, String projectId) {
        String miliToken = this.selectMiliToken();
        return this.miliService.registerByVisitor(fangkeRegisterVo,miliToken,projectId);
    }


    @Override
    public R<FaceCheckVo> faceUploadAndCheck(MultipartFile file, String projectId) {
        String miliToken = this.selectMiliToken();
        return this.miliService.faceUploadAndCheck(file,miliToken,projectId);
    }

    @Override
    public R<MiliPageData<FangkeLogVo>> selectFangkeLogPage(Integer current, Integer size, String name){

        String miliToken = this.selectMiliToken();
        return this.miliService.selectFangkeLogPage(current,size,name,miliToken);
    }


    @Override
    public R getAuthorizedFaceFk(String phone,String projectId){

        String miliToken = this.selectMiliToken();
        if(StringUtils.isBlank(phone)){
            GuanUserVo guanUserVo = this.guanUserService.selectUserByToken();
            phone = guanUserVo.getPhone();
        }
        return this.miliService.getAuthorizedFaceFk(phone,miliToken,projectId);
    }

    @Override
    public R<FangkeNotificationVo> visitorNotification( String projectId) {
        String miliToken = this.selectMiliToken();
        return this.miliService.visitorNotification(miliToken,projectId);
    }


    @Override
    public R<List<IndexFangkeLogVo>> selectIndexFangkeLogPage(String name,String projectId){
        String miliToken = this.selectMiliToken();
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = today.format(formatter);
        R<List<IndexFangkeLogVo>> listR = this.miliService.selectIndexFangkeLogPage(1, 500, name, formattedDate, "2", miliToken);
        return listR;
    }

    @Override
    public R<FangkeQrCodeVo> getFangkeQrCode(String visitId,String projectId){
        String miliToken = this.selectMiliToken();
        return this.miliService.getFangkeQrCode(visitId,miliToken,projectId);
    }

    @Override
    public R<FaceCheckVo> faceUploadAndCheckByBase64(String imageBase64, String projectId) {
        String miliToken = this.selectMiliToken();
        return this.miliService.faceUploadAndCheckByBase64(miliToken,imageBase64,projectId);
    }

    @Override
    public R<CheckPassRightVo> checkPassRight(String serialNumber) {
        GuanUserVo guanUserVo = this.guanUserService.selectUserByToken();
        String phone = guanUserVo.getPhone();
        Long guanUserVoId = guanUserVo.getId();
        String key = CacheKeyContants.GUANLIN_MILI_USER_TOKEN+guanUserVoId;

        String miliToken = (String)this.redisUtil.get(key);
        return this.miliService.selectVisitorCheckPassRight(phone,serialNumber,miliToken);
    }

    @Override
    public R<List<VisitorReasonVo>> selectVisitorLabel(String projectId) {
        String miliToken = this.selectMiliToken();
        R<List<VisitorReasonVo>> returnVo = this.miliService.selectVisitorLabel(miliToken ,projectId);
        return returnVo;
    }

    private String selectMiliToken(){
        GuanUserVo guanUserVo = this.guanUserService.selectUserByToken();
        Long guanUserVoId = guanUserVo.getId();
        String key = CacheKeyContants.GUANLIN_MILI_USER_TOKEN+guanUserVoId;

        String miliToken = (String)this.redisUtil.get(key);
        return miliToken;
    }
}
